package visiopuce.DAO;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.hibernate.Query;

import visiopuce.HibernateUtil;
import visiopuce.objets.TypeCotation;

public class TypeCotationDAOImpl extends GenericDAOImpl<TypeCotation, BigDecimal> implements TypeCotationDAO {

	/** Constructeur priv� */
	private TypeCotationDAOImpl() {
	}

	/** Instance unique non pr�initialis�e */
	private static TypeCotationDAOImpl INSTANCE = null;

	/** Point d'acc�s pour l'instance unique du singleton */
	public static TypeCotationDAO getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new TypeCotationDAOImpl();
		}
		return INSTANCE;
	}

	public ArrayList<TypeCotation> getTypeCotation(String libelle) {
		ArrayList<TypeCotation> types = null;
		String sql;
		Query query;
		if (libelle != null){
		 sql = "SELECT r FROM TypeCotation r WHERE r.libelle LIKE :libelle";
		query = HibernateUtil.getSession().createQuery(sql).setParameter("libelle", libelle);
		} else {
			 sql = "SELECT r FROM TypeCotation r";
			 query = HibernateUtil.getSession().createQuery(sql);
		}

		types = (ArrayList<TypeCotation>) query.list();
		return types;
	}

	public ArrayList<TypeCotation> getTypeCotationByTxt(String txt) {
		ArrayList<TypeCotation> types = null;
		String sql;
		sql = "SELECT r FROM TypeCotation r WHERE r.libelle LIKE :txt1 OR r.description LIKE :txt2";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("txt1", txt + "%").setParameter("txt2", "%" + txt + "%");
		types = (ArrayList<TypeCotation>) query.list();
		return types;
	}
}